<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
require_once("includes/include.php");
require_once("includes/datezoneset.php");
require_once('Downloadnew/Download.php');
require_once('helper/helper.php');
@session_start();
class blogManage extends blogdao{

	public $Downlod;
	public $helper;
	public $log;
	public function __construct(){
		parent::__construct();
		$this->Downlod = new Download();
		$this->helper = new Helper();
		$this->log = new Log();
	}
//	public function matchImgUrls($content){
//		$matches = preg_match_all("/<img\s*.*src=[\'\"]?(\w.*[\S])[\'\"]?\s*[\/]>/iU",$content,$matchesarray);
//		return $matchesarray[1];
//	}	
	
	
	public function submitDiary($data){
		$title = htmlspecialchars($data["title"]);
		$kind = htmlspecialchars($data["kind"]);
		$content = $data["editor1"];
		$quanxian = $data["quanxian"];
		$postdate = date("Y-m-d H:i:s");
		$postType = $data["postType"];
		$draftId = $data["draftId"];
		$userId = $_SESSION["userId"];
		$title = str_replace("0opc6","&",$title);
		$title = str_replace("'","&#39;",$title);
		$content = str_replace("0opc6","&",$content);
		$content = str_replace("O0Dkbol9","+",$content);
		$content = str_replace("O0Dkbol8","/",$content);
		$dao=new blogdao();
		if($postType==0){
			//$savedFilename = $this->helper->loadXmlFile();
			$savedFilename = (string)($this->helper->loadXmlFile()->blogimg->path);
			$content = $this->Downlod->download($content,$savedFilename);
			$addNewBlog = $dao->goAddNewBlog($title,$kind,$content,$postdate,$userId,$quanxian);
			if ($addNewBlog!=""){
				echo "<mes>addsuccess</mes>";
				echo "<blogId>$addNewBlog</blogId>";
			}
			else {
				echo "<mes>addfailed</mes>";
			}
		}
		else if($postType==1){
			$blogId =$data["blogId"];
			$this->deleteImages($blogId,$content);
			$savedFilename = $this->helper->loadXmlFile();
			$savedFilename = (string)($savedFilename->blogimg->path);
			$content = $this->Downlod->download($content,$savedFilename);
			$updateBlog = $this->updateBlog($title,$kind,$content,$postdate,$userId,$quanxian,$blogId);
			if ($updateBlog){
				echo "<mes>updatesuccess</mes>";
				echo "<blogId>$blogId</blogId>";
			}
			else {
				echo "<mes>updatefailed</mes>";
			}
		}else if ($postType==2){
			//add blog draft
			//disable download images when add blog draft
			//$savedFilename = (string)($this->helper->loadXmlFile()->blogimg->path);
			//$content = $this->Downlod->download($content,$savedFilename);
			if (trim($title)==""){
				$title="无标题";
			}
			$addNewBlogDraft = $dao->goAddBlogDraft($title,$kind,$content,$postdate,$userId,$quanxian,$draftId);
			$num = $dao->goGetBlogDraftId($userId);
			if ($addNewBlogDraft!=""){
				echo "<mes>addsuccess</mes>";
				echo "<num>$num</num>";
				echo "<blogId>$addNewBlogDraft</blogId>";
			}
			else {
				echo "<mes>addfailed</mes>";
			}
		}
	}
	
	/**
	 * 更新博客
	 *
	 * @param 标题 $title
	 * @param 类别 $kind
	 * @param 内容 $content
	 * @param 提交时间 $postdate
	 * @param 用户Id $userId
	 * @param 权限 $quanxian
	 * @param 原博文ID $blogId
	 * 
	 */
	public function updateBlog($title,$kind,$content,$postdate,$userId,$quanxian,$blogId){
		$dao =new blogdao();
		$query = $dao->goUpdateBlog($title,$kind,$content,$postdate,$userId,$quanxian,$blogId);
		return $query;
	}
	public function readBlogDraft(){
		$userId = $_SESSION["userId"];
		$dao =new blogdao();
		$query = $dao->goReadBlogDraft($userid);
		return $blogDraft;
	}
	function getLocalImages($imgstring){
		$imgstring = stripslashes($imgstring);
		$img_array = array();
		preg_match_all("/(src|SRC)=[\"|'| ]{0,}(\w(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$imgstring,$img_array);
		$img_array = array_unique($img_array[2]);
		//$img_array = $img_array[2];
		return $img_array;	
	}
	public function deleteImages($blogId,$newcontent){
		$content = $this->getTargetBlogContent($blogId);
		$imagesPaths = $this->getLocalImages($content);
		$newimagesPaths = $this->getLocalImages($newcontent);
		if (!empty($imagesPaths)){
			foreach ($imagesPaths as $path){
				clearstatcache();
				if (is_readable($path)&&!in_array($path,$newimagesPaths)){
					unlink($path);
				}else{
					$this->log->write("blog delete:image parth->".$path." can not be readed");
					//return false;
				}
			}
		}
		return true;
	}
		
}

$post = $_POST;
if (!empty($post)&&$post['postType']!=3&&$post['postType']!=4&&$post['postType']!=5) {
	$manage = new blogManage();
	$subMmit = $manage->submitDiary($post);
}else if(!empty($post)&&$post['postType']==3) {
	$manage = new blogManage();
	$blogDraft = $manage->readBlogDraft();
}else if(!empty($post)&&$post['postType']==4){
	$manage = new blogManage();
	
	$blogDraft = $manage->goReadTargetDraft($post['draftId']);
	if (!empty($blogDraft)){
		foreach ($blogDraft as $draft){
			//$draft['content'] = htmlspecialchars($draft['content'],ENT_QUOTES);
			//$draft['content'] = preg_replace("/\//","&#47;",$draft['content']);
			//$draft['content'] = str_replace("/","Koslsal90s",$draft['content']);
			echo "<title>".$draft['title']."</title>";
			echo "<kind>".$draft['kind']."</kind>";
			//echo "<content>".$draft['content']."</content>";
		}
	}
}else if ($post['postType']==5){
	$manage = new blogManage();
	$delete= $manage->goDeleteTargetDraft($post['draftId']);
	if ($delete){
		echo "<mes>1</mes>";
	}else{
		echo "<mes>0</mes>";
	}
}

echo '</response>';
?>